class GeozoneRectangleController {

    def index = {redirect(action: list, params: params)}

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [geozoneRectangleList: GeozoneRectangle.list(params)]
    }

    def show = {
        def geozoneRectangle = GeozoneRectangle.get(params.id)

        if (!geozoneRectangle) {
            flash.message = "GeozoneRectangle not found with id ${params.id}"
            redirect(action: list)
        }
        else {return [geozoneRectangle: geozoneRectangle]}
    }

    def delete = {
        def geozoneRectangle = GeozoneRectangle.get(params.id)
        if (geozoneRectangle) {
            geozoneRectangle.delete()
            flash.message = "GeozoneRectangle ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "GeozoneRectangle not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def geozoneRectangle = GeozoneRectangle.get(params.id)

        if (!geozoneRectangle) {
            flash.message = "GeozoneRectangle not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [geozoneRectangle: geozoneRectangle]
        }
    }

    def update = {
        def geozoneRectangle = GeozoneRectangle.get(params.id)
        if (geozoneRectangle) {
            geozoneRectangle.properties = params
            if (!geozoneRectangle.hasErrors() && geozoneRectangle.save()) {
                flash.message = "GeozoneRectangle ${params.id} updated"
                redirect(action: show, id: geozoneRectangle.id)
            }
            else {
                render(view: 'edit', model: [geozoneRectangle: geozoneRectangle])
            }
        }
        else {
            flash.message = "GeozoneRectangle not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def geozoneRectangle = new GeozoneRectangle()
        geozoneRectangle.properties = params
        return ['geozoneRectangle': geozoneRectangle]
    }

    def save = {
        def geozoneRectangle = new GeozoneRectangle(params)
        println params

        def lat1 = params.latitude1
        def lng1 = params.longitude1
        def lat2 = params.latitude2
        def lng2 = params.longitude2

        def p1 = new Point("latitude": lat1, "longitude": lng1, "zoneType":"Rectangle")
        def p2 = new Point("latitude": lat2, "longitude": lng2,"zoneType":"Rectangle")
        geozoneRectangle.addToPoints(p1).addToPoints(p2)


        if (!geozoneRectangle.hasErrors() && geozoneRectangle.save()) {
            flash.message = "GeozoneRectangle ${geozoneRectangle.id} created"
            redirect(action: show, id: geozoneRectangle.id)
        }
        else {
            render(view: 'create', model: [geozoneRectangle: geozoneRectangle])
        }
    }
}